package com.juanpi.order.configuration;

import com.juanpi.order.common.utils.HttpClientUtil;
import com.juanpi.order.common.utils.JsonMapper;
import com.juanpi.order.configuration.bean.KafkaConfigBean;
import com.juanpi.order.configuration.bean.KafkaTopic;
import com.juanpi.order.configuration.dao.KafkaConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xueqing
 *         Created on 2017/2/8.
 */
@Service
public class KafkaConfigService {

    @Autowired
    private KafkaConfigMapper kafkaConfigMapper;

    public void saveKafkaConfig(KafkaConfigBean kafkaConfigBean){
        kafkaConfigMapper.saveKafkaConfig(kafkaConfigBean);
    }

    /**
     * 分为两步，第一步读取
     * @param kafkaName
     * @return
     */
    public List<KafkaTopic> findKafkaTopic(String kafkaName){
        List<KafkaTopic> result = new ArrayList<>();
        KafkaConfigBean kafkaConfigBean = kafkaConfigMapper.queryKafkaConfigByKafkaName(kafkaName);
        if(kafkaConfigBean==null){
            return result;
        }

        String kafkaTopicData = HttpClientUtil.doGet("url", new HashMap<String, String>());
        result = JsonMapper.fromJson(kafkaTopicData, List.class);
        return result;
    }
}
